﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SQLitePCL;
using System.Drawing.Printing;
using System.Text;
using System.Xml.Linq;
using Warehouse.Entites;
using Warehouse.IServices.Conveyorlines;
using Warehouse.Services;
using Warehouse.Services.Conveyorlines;
using WCS.Entities;
using WCS.Entities.Api;
using WCS.Entities.BulkStorage;
using WCS.Entities.Enum;
using WCS.IServices;
using WCS.Services;
using WCS.ViewModels;

namespace Juionwarehouse.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ConveyorController : ControllerBase
    {
        private readonly IConveyorIServices Con;
        private readonly IDeviceManager _deviceManager;
        private readonly ILogger<ConveyorServices> logger;
        //private readonly TcpClientReadCode tcp;
        public ConveyorController(IConveyorIServices con, IDeviceManager _deviceManager, ILogger<ConveyorServices> logger)
        {
            Con = con;
            this._deviceManager = _deviceManager;
            //this.tcp = tcp;
            this.logger = logger;
        }
        //机械臂码
        [HttpPost]
        public ApiResult Stack(int id, int type)
        {
            Con.Stacking(id, type);
            return ApiResultHelp.Success("下发成功");
        }
        //机械臂拆
        [HttpGet]
        public ApiResult StackPLC(int id, int plcValue)
        {
            Con.Conveyor(id, plcValue);
            return ApiResultHelp.Success("下发成功");
        }
        //[HttpPost]
        //public ApiResult Roboticarm(string type, int number)
        //{
        //    Con.Roboticarm(type,number);
        //    return ApiResultHelp.Success("下发成功");
        //}
        [HttpPost]
        public ApiResult Kuangshi(int number)
        {
            var a = _deviceManager.Ports.FirstOrDefault(i => i.DeviceId == "11001");
            a?.BelongDevice.Operation.Write("133", (short)1);
            var res = a?.BelongDevice.Operation.ReadInt16("110");
            if (res != null && res.Value != 0)
            {
                var a1 = a?.BelongDevice.Operation.Write("112", (short)11054);
                var a2 = a?.BelongDevice.Operation.Write("114", (short)number);
            }
            return ApiResultHelp.Success(1);
        }
        //[HttpPost]
        //public IActionResult BulkStorage1(int numbell)
        //{

        //    var a = _deviceManager.Ports.FirstOrDefault(i => i.DeviceId == "11059");
        //    var res = a?.BelongDevice.Operation.ReadInt32("110");
        //    if (res != null && res.Value == 1)
        //    {
        //        a?.BelongDevice.Operation.Write("112", (short)11058);
        //        a?.BelongDevice.Operation.Write("114", (short)numbell);
        //    }
        //    var list = a?.BelongDevice.Operation.ReadString("6560", 8);
        //    return Ok(list.Value);
        //}
        [HttpPost]
        public IActionResult A1(int numbell)
        {
            var a = _deviceManager.Ports.FirstOrDefault(i => i.DeviceId == "11001");
            var res = a?.BelongDevice.Operation.ReadInt16("490");
            if (res != null && res.Value != 0)
            {
                a?.BelongDevice.Operation.Write("492", (short)11033);
                a?.BelongDevice.Operation.Write("494", (short)numbell);
            }
            return Ok(1);
        }

        //散库入
        //[HttpGet]
        //public ApiResult SA(int numbell)
        //{
        //    Con.Job15(numbell);
        //    return ApiResultHelp.Success("下发成功");
        //}

        //机械臂
        [HttpPost]
        public ApiResult BulkProduce(BulkProduce model)
        {
            
            try
            {
                var transPort = _deviceManager.Ports.FirstOrDefault(i => i.DeviceId == "11001");
                // 不知道是什么任务号
                var res = transPort?.BelongDevice.Operation.ReadInt16("450");
                // 不知道是个什么
                var res1 = transPort?.BelongDevice.Operation.ReadInt16("458");
                var num = transPort?.BelongDevice.Operation.ReadInt16("3715");
                var num2 = transPort?.BelongDevice.Operation.ReadInt16("3716");
                // 机械臂当前处于什么状态
                var fan = transPort?.BelongDevice.Operation.ReadInt16("3722");
                if (fan?.Value == 0 && res?.Value != 2 && num.Value==0 && num2.Value==0)
                {

                    Con.Stacking(model.Name == "210" ? 2 : 1, model.aum);
                    logger.LogInformation($"机械臂当前的状态{fan.Value},当前线体是否有托盘{res.Value},当前机械臂码货的类型1{num.Value},当前码货的类型2{num2.Value}");
                    return ApiResultHelp.Success("成功");
                }
                return ApiResultHelp.Error("发送失败");
                

            }
            catch (Exception ex)
            {
                return ApiResultHelp.Error($"接口异常，异常信息：{ex.Message}");
            }
        }

        [HttpPost]
        public ApiResult BulkInformation(BulkInformationViewModel bulkInformation)
        {
            try
            {
                if (bulkInformation != null)
                {
                    if (!string.IsNullOrEmpty(bulkInformation.MaterialNumber))
                    {
                        Con.BulkInformation(bulkInformation);
                        return ApiResultHelp.Success("成功");
                    }
                }
            }
            catch (Exception ex)
            {
                ApiResultHelp.Error($"发送失败,Error info:{ex.Message}");
            }

            return ApiResultHelp.Error("发送失败");
        }


        [HttpPost]
        public IActionResult Ma()
        {
            var a = _deviceManager.Ports.FirstOrDefault(i => i.DeviceId == "11");
            if (a?.BelongDevice.Operation != null)
            {
                var res = a?.BelongDevice.Operation.ToString();
                return Ok(res);
            }
            return Ok(1);
        }

        //[HttpGet]
        //public IActionResult XIAN(int numbell)
        //{
        //    return Ok(Con.GetMaterialTypeByMission(numbell));
        //}

        //[HttpPost]
        //public ApiResult stock()
        //{
        //    Con.BulkStorage();
        //    return ApiResultHelp.Success();
        //}
    }

}
